package com.hwtx.form.domain.def;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

public class FormValidation {

    static class RequiredPredicate implements FromValidatePredicate<Object> {

        final boolean required = false;

        @Override
        public boolean test(Object s) {
            return !required || s != null;
        }

        @Override
        public List<Object> getRuleValue() {
            return Collections.singletonList(required);
        }
    }

    @AllArgsConstructor
    static class IsAlphaPredicate implements FromValidatePredicate<String> {
        Boolean isAlpha;

        @Override
        public boolean test(String s) {
            return !isAlpha || StringUtils.isAlpha(s);
        }

        @Override
        public List<Object> getRuleValue() {
            return Collections.singletonList(isAlpha);
        }
    }

    @AllArgsConstructor
    static class MaxLengthPredicate implements FromValidatePredicate<String> {
        Integer maxLength;

        @Override
        public boolean test(String s) {
            return !StringUtils.isNotEmpty(s) || s.length() <= maxLength;
        }

        @Override
        public List<Object> getRuleValue() {
            return Collections.singletonList(maxLength);
        }
    }

    @AllArgsConstructor
    static class MinLengthPredicate implements FromValidatePredicate<String> {
        Integer minLength;

        @Override
        public boolean test(String s) {
            return !StringUtils.isNotEmpty(s) || s.length() >= minLength;
        }

        @Override
        public List<Object> getRuleValue() {
            return Collections.singletonList(minLength);
        }
    }

    @AllArgsConstructor
    static class IsNumericPredicate implements FromValidatePredicate<String> {
        Boolean isNumeric;

        @Override
        public boolean test(String s) {
            return !isNumeric || StringUtils.isNumeric(s);
        }

        @Override
        public List<Object> getRuleValue() {
            return Collections.singletonList(isNumeric);
        }
    }

    @AllArgsConstructor
    static class MaximumPredicate implements FromValidatePredicate<String> {
        Integer maximum;

        @Override
        public boolean test(String s) {
            return !StringUtils.isNotEmpty(s) || !StringUtils.isNumeric(s) || Integer.parseInt(s) < maximum;
        }

        @Override
        public List<Object> getRuleValue() {
            return Collections.singletonList(maximum);
        }
    }

    @AllArgsConstructor
    static class MinimumPredicate implements FromValidatePredicate<String> {
        Double minimum;

        @Override
        public boolean test(String s) {
            return !StringUtils.isNotEmpty(s) || !StringUtils.isNumeric(s) || Double.parseDouble(s) > minimum;
        }

        @Override
        public List<Object> getRuleValue() {
            return Collections.singletonList(minimum);
        }
    }

    @AllArgsConstructor
    static class IsPhoneNumberPredicate implements FromValidatePredicate<String> {
        Boolean isPhoneNumber;

        @Override
        public boolean test(String s) {
            return !isPhoneNumber || s.matches("^1[3-9]\\d{9}$");
        }

        @Override
        public List<Object> getRuleValue() {
            return Collections.singletonList(isPhoneNumber);
        }
    }

    @AllArgsConstructor
    static class IsEmailPredicate implements FromValidatePredicate<String> {
        Boolean isEmail;

        @Override
        public boolean test(String s) {
            return !isEmail || s.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
        }

        @Override
        public List<Object> getRuleValue() {
            return Collections.singletonList(isEmail);
        }
    }

}
